package com.gitee.xzb617.mybatis.sqllog.innerinvoke;

import com.gitee.xzb617.mybatis.sqllog.SqlLogProperties;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import java.util.List;

@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class InnerInvokeSqlLogAspectj {

    private final SqlLogProperties sqlLogProperties;

    public InnerInvokeSqlLogAspectj(SqlLogProperties sqlLogProperties) {
        this.sqlLogProperties = sqlLogProperties;
    }

    @Pointcut("@annotation(com.gitee.xzb617.mybatis.sqllog.innerinvoke.InnerInvokeSqlLog)")
    public void pointCut(){}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 判断当前环境的内部调用有没有配置执行对象的类路径，没有则添加
        List<String> innerInvokeMatchRules = this.sqlLogProperties.getInnerInvokeMatchRules();
        String className = pjp.getTarget().getClass().getName();
        if (!innerInvokeMatchRules.contains(className)) {
            innerInvokeMatchRules.add(className);
        }
        // 继续执行
        return pjp.proceed();
    }

}
